﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;

namespace Cobit5.BIZ.Cobit
{
    public class RACIRole
    {
        #region Model
        private int _id;
        private string _rolename;
        private string _rolenamechn;
        /// <summary>
        /// 
        /// </summary>
        public int id
        {
            set { _id = value; }
            get { return _id; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string roleName
        {
            set { _rolename = value; }
            get { return _rolename; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string roleNameCHN
        {
            set { _rolenamechn = value; }
            get { return _rolenamechn; }
        }
        #endregion Model

        #region RACIRoleFunction

        /// <summary>
        /// Initialize
        /// </summary>
        public RACIRole(int in_id)
        {
            if (RACIRole.ExistRACIRole(in_id) == 0)
                return;

            Model.RACI_Role myRole = new Model.RACI_Role();
            DAL.RACI_Role optRole = new DAL.RACI_Role();
            myRole = optRole.GetModel(in_id);

            this._id = myRole.id;
            this._rolename = myRole.roleName;
            this._rolenamechn = myRole.roleNameCHN;
        }

        /// <summary>
        /// Count the number of RACIRole
        /// </summary>
        public static int CountAllRACIRole()
        {
            DAL.RACI_Role optRole = new DAL.RACI_Role();
            return optRole.GetRecordCount("1=1");
        }

        /// <summary>
        /// List all the RACIRole in pages
        /// </summary>
        public static List<RACIRole> GetAllRACIRole(string in_orderby, int in_startIndex, int in_endIndex)
        {
            DAL.RACI_Role optRACI = new DAL.RACI_Role();
            DataSet ds = optRACI.GetListByPage("1=1", in_orderby, in_startIndex, in_endIndex);

            /*** RACI Role not exists ***/
            if (ds == null || ds.Tables[0].Rows.Count < 1)
                return null;

            List<RACIRole> returnListRACIRole = new List<RACIRole>();
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                RACIRole newRACIRole = new RACIRole(int.Parse(ds.Tables[0].Rows[i]["id"].ToString()));
                returnListRACIRole.Add(newRACIRole);
            }

            return returnListRACIRole;
        }

        /// <summary>
        /// Check if the RACIRole exists
        /// Return  1:Exist;
        ///         0:Not Exist;
        /// </summary>
        public static int ExistRACIRole(int in_id)
        {
            DAL.RACI_Role optRole = new DAL.RACI_Role();
            if (!optRole.Exists(in_id))
                return 0;
            else return 1;
        }

        /// <summary>
        /// Create a new GoalProcess
        /// Return  1.Success
        ///         2.Id already existed
        ///         3.DAL Error
        /// </summary>
        public static int CreateRACIRole(int in_id, string in_roleName, string in_roleNameCHN)
        {
            if (RACIRole.ExistRACIRole(in_id) == 1)
                return 2;

            Model.RACI_Role myRACI = new Model.RACI_Role();
            myRACI.id = in_id;
            myRACI.roleName = in_roleName;
            myRACI.roleNameCHN = in_roleNameCHN;

            DAL.RACI_Role optRACI = new DAL.RACI_Role();
            bool ok = optRACI.Add(myRACI);
            if (!ok) return 3;
            else return 1;
        }

        /// <summary>
        /// Update the Process
        /// Return: 1.Success
        ///         2.DAL Error
        /// </summary>
        public int UpdateRACIRole()
        {
            Model.RACI_Role myRACI = new Model.RACI_Role();
            myRACI.id = this._id;
            myRACI.roleName = this._rolename;
            myRACI.roleNameCHN = this._rolenamechn;

            DAL.RACI_Role optRACI = new DAL.RACI_Role();
            bool ok = optRACI.Update(myRACI);
            if (!ok) return 2;
            else return 1;
        }

        /// <summary>
        /// Delete one RACIRole
        /// Return: 1.Success
        ///         2.RACIRole doesn't exist
        ///         3.DAL Error in deleting the realtionship with Membership
        /// </summary>
        public static int DeleteRACIRole(int in_roleID)
        {
            /*** If roldID exist ***/
            if (RACIRole.ExistRACIRole(in_roleID) == 0)
                return 2;

            /*** Delete items in RACI_Body ***/
            DAL.RACI_Body optRACI_Body = new DAL.RACI_Body();
            DataSet ds = optRACI_Body.GetList("roleID='" + in_roleID + "'");
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    bool ok = optRACI_Body.Delete(in_roleID, ds.Tables[0].Rows[i]["userID"].ToString());
                    if (!ok) return 3;
                }
            }

            /*** Delete items in RACI ***/
            DAL.RACI optRACI = new DAL.RACI();
            ds.Clear();
            ds = optRACI.GetList("roleID='" + in_roleID + "'");
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    bool ok = optRACI.Delete(ds.Tables[0].Rows[i]["practiceID"].ToString(), in_roleID);
                    if (!ok) return 3;
                }
            }

            /*** Delete RACI_Role ***/
            DAL.RACI_Role optRACI_Role = new DAL.RACI_Role();
            bool ok1 = optRACI_Role.Delete(in_roleID);
            if (!ok1) return 3;
            else return 1;
        }

        #endregion RACIRoleFunction

        #region RACI-Practice

        /// <summary>
        /// Get the number of Practices that belong to this RACI Role
        /// </summary>
        public int CountPractice()
        {
            DAL.RACI optRACI = new DAL.RACI();
            return optRACI.GetRecordCount("roleID='" + this._id + "'");
        }

        /// <summary>
        /// Get the number of Practices that belong to this RACI Role
        /// </summary>
        public int CountPractice(string in_raci)
        {
            DAL.RACI optRACI = new DAL.RACI();
            return optRACI.GetRecordCount("roleID='" + this._id + "' and raci='" + in_raci + "'");
        }

        /// <summary>
        /// Get the list of Practices
        /// Parameters: 1.in_orderBy:"practiceID","roleID","raci"
        /// </summary>
        public List<Practice> Practices(string in_orderBy, int in_startIndex, int in_endIndex)
        {
            DAL.RACI optRACI = new DAL.RACI();
            DataSet ds = optRACI.GetListByPage("roleID='" + this._id + "'", in_orderBy, in_startIndex, in_endIndex);
            if (ds == null || ds.Tables[0].Rows.Count < 1)
                return null;

            List<Practice> returnPractices = new List<Practice>();
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                Practice newPractice = new Practice(ds.Tables[0].Rows[i]["practiceID"].ToString());
                returnPractices.Add(newPractice);
            }

            return returnPractices;
        }

        /// <summary>
        /// Get the list of Practices
        /// Parameters: 1.in_orderBy:"practiceID","roleID","raci"
        /// </summary>
        public List<Practice> Practices(string in_raci, string in_orderBy, int in_startIndex, int in_endIndex)
        {
            DAL.RACI optRACI = new DAL.RACI();
            DataSet ds = optRACI.GetListByPage("roleID='" + this._id + "' and raci='" + in_raci + "'", in_orderBy, in_startIndex, in_endIndex);
            if (ds == null || ds.Tables[0].Rows.Count < 1)
                return null;

            List<Practice> returnPractices = new List<Practice>();
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                Practice newPractice = new Practice(ds.Tables[0].Rows[i]["practiceID"].ToString());
                returnPractices.Add(newPractice);
            }

            return returnPractices;
        }

        /// <summary>
        /// Add a Practice to this RACIRole
        /// Parameters: 1.in_pracitceID: Practice ID
        ///             2.in_raci: R A C or I
        /// Return: 2.Practice not exists
        ///         3.Error in DAL
        ///         1.Success
        /// </summary>
        public int AddPractice(string in_practiceID, string in_raci)
        {
            if (Practice.ExistPractice(in_practiceID) == 0)
                return 2;

            Model.RACI myRACI = new Model.RACI();
            myRACI.practiceID = in_practiceID;
            myRACI.roleID = this._id;
            myRACI.raci = in_raci;

            DAL.RACI optRACI = new DAL.RACI();
            bool ok = optRACI.Add(myRACI);
            if (!ok) return 3;
            else return 1;
        }

        /// <summary>
        /// Remove a Practice from this RACI Role
        /// Parameters: 1.in_practiceID: Practice id
        /// Return: 2.Practice not exists
        ///         3.Error in DAL
        ///         1.Success
        /// </summary>
        public int RemovePractice(string in_practiceID)
        {
            if (Practice.ExistPractice(in_practiceID) == 0)
                return 2;

            DAL.RACI optRACI = new DAL.RACI();
            bool ok = optRACI.Delete(in_practiceID, this._id);
            if (!ok) return 3;
            else return 1;
        }

        #endregion RACI-Practice

        #region RACI-Member

        /// <summary>
        /// Get the number of Member that belongs to this Process
        /// </summary>
        public int CountMembership()
        {
            DAL.RACI_Body optRACI = new DAL.RACI_Body();
            return optRACI.GetRecordCount("roleID='" + this._id + "'");
        }

        /// <summary>
        /// Get the list of Membership
        /// Parameters: 1.in_orderBy:"userID","roleID"
        /// </summary>
        public List<MemberShip.Membership> Memberships(string in_orderBy, int in_startIndex, int in_endIndex)
        {
            DAL.RACI_Body optRACI_Body = new DAL.RACI_Body();
            DataSet ds = optRACI_Body.GetListByPage("roleID='" + this._id + "'", in_orderBy, in_startIndex, in_endIndex);
            if (ds == null || ds.Tables[0].Rows.Count < 1)
                return null;

            List<MemberShip.Membership> returnMemberships = new List<MemberShip.Membership>();
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                MemberShip.Membership newMembership = new MemberShip.Membership(ds.Tables[0].Rows[i]["userID"].ToString());
                returnMemberships.Add(newMembership);
            }

            return returnMemberships;
        }

        /// <summary>
        /// Add a Membership to this RACI Role
        /// Parameters: 1.in_userID: Membership ID
        ///
        /// Return: 2.Membership not exists
        ///         3.Error in DAL
        ///         1.Success
        /// </summary>
        public int AddMembership(string in_userID)
        {
            if (MemberShip.Membership.ExistMembership(in_userID) == 0)
                return 2;

            Model.RACI_Body myRACI_Body = new Model.RACI_Body();
            myRACI_Body.roleID = this._id;
            myRACI_Body.userID = in_userID;

            DAL.RACI_Body optRACI_Body = new DAL.RACI_Body();
            bool ok = optRACI_Body.Add(myRACI_Body);
            if (!ok) return 3;
            else return 1;
        }

        /// <summary>
        /// Remove a Membership from this RACI Role
        /// Parameters: 1.in_userID: Membership id
        /// Return: 2.Membership not exists
        ///         3.Error in DAL
        ///         1.Success
        /// </summary>
        public int RemoveMembership(string in_userID)
        {
            if (MemberShip.Membership.ExistMembership(in_userID) == 0)
                return 2;

            DAL.RACI_Body optRACI_Body = new DAL.RACI_Body();
            bool ok = optRACI_Body.Delete(this._id, in_userID);
            if (!ok) return 3;
            else return 1;
        }

        #endregion RACI-Member

    }
}
